<?php
namespace product\service;

use product\service\interfaces\IGradeService;
use common\service\CommonService;
use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;
use herosphp\db\mysql\MysqlQueryBuilder;

Loader::import('product.service.interfaces.IGradeService');

/**
 * product(Service)接口实现
 * @package product\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class GradeService extends CommonService implements IGradeService {

    //重写count方法，用来报价管理->自定义牌号搜索
    public function count($conditions){

        if($conditions['username']) {

            $username=$conditions['username'];

            unset($conditions['username']);
            
            foreach($conditions as $k=>$condition){

                $conditions['grade`.`'.$k]=$condition;
                unset($conditions[$k]);
            }

            $userModel = Loader::model('User');

            $wheresql=$conditions = MysqlQueryBuilder::buildConditions($conditions);

            $sql="Select * from grade Left Join user On grade.saas_user_id=user.id Where {$wheresql} And user.name='{$username}'";

            $counts=$userModel->getDB()->excute($sql)->fetchColumn();

            return $counts;

        }
        return parent::count($conditions);
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        //初始化分类，品名，牌号
        $typeModel = Loader::model("productType");
        $brandModel = Loader::model("Brand");
        $factoryModel = Loader::model("Factory");
        $managerModel = Loader::model('Manager');
        $userModel = Loader::model('User');

        if($conditions['username']){

            $user = $userModel->getItem(array('name' =>$conditions['username']));

            $conditions['saas_user_id']=$user['id'];
            unset($conditions['username']);
        }


        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if ( !empty($items) ) {
            $typeIds = array();
            $brandIds = array();
            $factoryIds = array();
            $managerIds = array();
            $userIds = array();

            foreach ( $items as $val ) {
                $typeIds[] = $val['typeid'];
                $brandIds[] = $val['brandid'];
                $factoryIds[] = $val['facid'];
                $managerIds[] = $val['submiter'];
                $managerIds[] = $val['checker'];
                $userIds[] = $val['saas_user_id'];
            }

            $types = $typeModel->getItems(array('id' => array('$in' => $typeIds)));
            $brands = $brandModel->getItems(array('id' => array('$in' => $brandIds)));
            $factorys = $factoryModel->getItems(array('id' => array('$in' => $factoryIds)));
            $managers = $managerModel->getItems(array('id' => array('$in' => $managerIds)));
            $users = $userModel->getItems(array('id' => array('$in' => $userIds)));

            $types = ArrayUtils::changeArrayKey($types, 'id');
            $brands = ArrayUtils::changeArrayKey($brands, 'id');
            $factorys = ArrayUtils::changeArrayKey($factorys, 'id');
            $managers = ArrayUtils::changeArrayKey($managers, 'id');
            $users = ArrayUtils::changeArrayKey($users, 'id');

            foreach ( $items as $key => $value ) {
                $items[$key]['typename'] = $types[$value['typeid']]['name'];
                $items[$key]['brand'] = $brands[$value['brandid']]['name'];
                $items[$key]['factory'] = $factorys[$value['facid']]['name'];
                $items[$key]['submiter_name'] = $managers[$value['submiter']]['name'];
                $items[$key]['checker_name'] = $managers[$value['checker']]['name'];
                $items[$key]['saas_user_name'] = $users[$value['saas_user_id']]['name'];
                $items[$key]['saas_user_username'] = $users[$value['saas_user_id']]['username'];

            }
        }

        return $items;
    }
}
